package cn.iocoder.yudao.module.member.controller.admin.scoretype;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.scoretype.vo.*;
import cn.iocoder.yudao.module.member.convert.scoretype.ScoreTypeConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.scoretype.ScoreTypeDO;
import cn.iocoder.yudao.module.member.service.scoretype.ScoreTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 积分用途")
@RestController
@RequestMapping("/member/score-type")
@Validated
public class ScoreTypeController {

    @Resource
    private ScoreTypeService scoreTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建积分用途")
    @PreAuthorize("@ss.hasPermission('member:score-type:create')")
    public CommonResult<Long> createScoreType(@Valid @RequestBody ScoreTypeCreateReqVO createReqVO) {
        return success(scoreTypeService.createScoreType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新积分用途")
    @PreAuthorize("@ss.hasPermission('member:score-type:update')")
    public CommonResult<Boolean> updateScoreType(@Valid @RequestBody ScoreTypeUpdateReqVO updateReqVO) {
        scoreTypeService.updateScoreType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除积分用途")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:score-type:delete')")
    public CommonResult<Boolean> deleteScoreType(@RequestParam("id") Long id) {
        scoreTypeService.deleteScoreType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得积分用途")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:score-type:query')")
    public CommonResult<ScoreTypeRespVO> getScoreType(@RequestParam("id") Long id) {
        ScoreTypeDO scoreType = scoreTypeService.getScoreType(id);
        return success(ScoreTypeConvert.INSTANCE.convert(scoreType));
    }

    @GetMapping("/list")
    @Operation(summary = "获得积分用途列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:score-type:query')")
    public CommonResult<List<ScoreTypeRespVO>> getScoreTypeList(@RequestParam("ids") Collection<Long> ids) {
        List<ScoreTypeDO> list = scoreTypeService.getScoreTypeList(ids);
        return success(ScoreTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得积分用途分页")
    @PreAuthorize("@ss.hasPermission('member:score-type:query')")
    public CommonResult<PageResult<ScoreTypeRespVO>> getScoreTypePage(@Valid ScoreTypePageReqVO pageVO) {
        PageResult<ScoreTypeDO> pageResult = scoreTypeService.getScoreTypePage(pageVO);
        return success(ScoreTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出积分用途 Excel")
    @PreAuthorize("@ss.hasPermission('member:score-type:export')")
    @OperateLog(type = EXPORT)
    public void exportScoreTypeExcel(@Valid ScoreTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ScoreTypeDO> list = scoreTypeService.getScoreTypeList(exportReqVO);
        // 导出 Excel
        List<ScoreTypeExcelVO> datas = ScoreTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "积分用途.xls", "数据", ScoreTypeExcelVO.class, datas);
    }

}
